package com.linkage.jz.bankruptcy.activity.Project;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Main.CreateAddrActivity;
import com.linkage.jz.bankruptcy.activity.RegistLogin.GetPwdOneActivity;
import com.linkage.jz.bankruptcy.activity.RegistLogin.PhoneLoginActivity;
import com.linkage.jz.bankruptcy.adapter.FullyGridLayoutManager;
import com.linkage.jz.bankruptcy.adapter.GridImageAdapter;
import com.linkage.jz.bankruptcy.adapter.GridImagesAdapter;
import com.linkage.jz.bankruptcy.base.BaseActivity;
import com.linkage.jz.bankruptcy.base.BaseCreateActivity;
import com.linkage.jz.bankruptcy.bean.LoginInfo;
import com.linkage.jz.bankruptcy.bean.MyProInfo;
import com.linkage.jz.bankruptcy.bean.TokenInfo;
import com.linkage.jz.bankruptcy.utils.FileUtil;
import com.linkage.jz.bankruptcy.utils.SPUtils;
import com.linkage.jz.bankruptcy.utils.SignUtil;
import com.linkage.jz.bankruptcy.widget.PublicNoticeDialog;
import com.linkage.jz.bankruptcy.widget.ShapeLoadingDialog;
import com.linkage.seapeak.activity.user.ImageShowActivity;
import com.linkage.seapeak.fragment.ExamineAddDataFragment;
import com.linkage.seapeak.pojo.UploadImage;
import com.linkage.seapeak.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import top.zibin.luban.CompressionPredicate;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;


/**
 * A login screen that offers login via email/password.
 */
public class CreateProjectDetailThreeActivity extends BaseCreateActivity implements OnClickListener {
    private static final String TAG = CreateProjectDetailThreeActivity.class.getSimpleName();

    private ShapeLoadingDialog shapeLoadingDialog;
    private RecyclerView mRecyclerView, mRecyclerView2, mRecyclerView4, mRecyclerView5;
    private GridImagesAdapter adapter, adapter2, adapter4, adapter5;

    private int addImageType = -1;
    private RelativeLayout rl_addlogo, rl_addlogo2, rl_addlogo4, rl_addlogo5;

    private List<UploadImage> urlList1 = new ArrayList<>();
    private List<UploadImage> urlList2 = new ArrayList<>();
    private List<UploadImage> urlList4 = new ArrayList<>();
    private List<UploadImage> urlList5 = new ArrayList<>();
    private RelativeLayout ll_zwr_db_addr;

    private int maxSelectNum = 27;

    @Override
    public void initData() {

    }

    @Override
    public void setListener() {
//        btn_login.setOnClickListener(this);
        top_back.setOnClickListener(this);
//        ll_phonelogin.setOnClickListener(this);
//        getPwd.setOnClickListener(this);
        top_mune1.setOnClickListener(this);
        top_mune2.setOnClickListener(this);
        rl_addlogo.setOnClickListener(this);
        rl_addlogo2.setOnClickListener(this);
        rl_addlogo4.setOnClickListener(this);
        rl_addlogo5.setOnClickListener(this);
        ll_zwr_db_addr.setOnClickListener(this);
    }

    @Override
    public void initTitle() {
        top_title = findViewById(R.id.top_title);
        if (m_addtype == 1) {
            top_title.setText("新建项目");
        } else {
            top_title.setText("编辑项目");
        }
//        top_mune.setVisibility(View.INVISIBLE);
    }

    @Override
    public void initView() {

    }

    ImageView mLeftLogo;
    ImageView mRightLogo;
    EditText et_username,item_zwr_db;
    EditText et_password;
    Button btn_login;
    TextView top_title, getPwd,item_zwr_db_addr;
    String addressId = "";
    Button top_back;
    TextView top_mune1, top_mune2;
    CheckBox ck_remember;
    LinearLayout ll_charger, ll_admin, qqlogin;

    private static MyProInfo mInfo;
    private static int m_addtype;

    public static void start(Context context, MyProInfo info, int addtype) {
        Intent intent = new Intent(context, CreateProjectDetailThreeActivity.class);
        context.startActivity(intent);
        mInfo = info;
        m_addtype = addtype;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_create_pro_detail_three);

        initview();
        initWidget();
        initWidget2();
        initWidget4();
        initWidget5();
        String user = SPUtils.getInstance(Consts.SPNAME).getString("loginUser");
        String pwd = SPUtils.getInstance(Consts.SPNAME).getString("loginPwd");

        shapeLoadingDialog = new ShapeLoadingDialog(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void imageSizeChange(int tag, int size) {
        addImageType = tag;
    }

    //初始化视图
    private void initview() {
        //登录层、下拉层、其它登录方式层
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        item_zwr_db_addr = findViewById(R.id.item_zwr_db_addr);
        item_zwr_db = findViewById(R.id.item_zwr_db);
        top_back = findViewById(R.id.top_back);
        top_mune1 = findViewById(R.id.top_mune1);
        top_mune2 = findViewById(R.id.top_mune2);
        ll_zwr_db_addr = findViewById(R.id.ll_zwr_db_addr);
        getPwd = findViewById(R.id.getPwd);
        ck_remember = findViewById(R.id.ck_remember);
        if (m_addtype == 2) {
            top_mune2.setVisibility(View.INVISIBLE);
        }

        mRecyclerView = findViewById(R.id.recycler);
        mRecyclerView2 = findViewById(R.id.recycler2);
        mRecyclerView4 = findViewById(R.id.recycler4);
        mRecyclerView5 = findViewById(R.id.recycler5);

        rl_addlogo = findViewById(R.id.rl_addlogo);
        rl_addlogo2 = findViewById(R.id.rl_addlogo2);
        rl_addlogo4 = findViewById(R.id.rl_addlogo4);
        rl_addlogo5 = findViewById(R.id.rl_addlogo5);


    }
    private int REQUEST_CODE_ADDRESS = 10001;

    @Override
    public void onClick(View view) {

        String phone = "";
        String code = "";
        String pwd = "";
        switch (view.getId()) {
            case R.id.btn_login:
                //返回
//                MainActivity.start(this);
//                finish();
                phone = et_username.getText().toString();
//                    code = et_vcode.getText().toString();
                pwd = et_password.getText().toString();
                if ("".equalsIgnoreCase(phone)) {
                    Toast.makeText(this, "手机号码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("".equalsIgnoreCase(pwd)) {
                    Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
//                    LoginUser(phone, code, pwd);
                break;
            case R.id.ll_phonelogin:
                PhoneLoginActivity.start(this);
                finish();
                break;
            case R.id.top_back:
                dialogType =1;
                noticeDialog = new PublicNoticeDialog(CreateProjectDetailThreeActivity.this,onClickListener,
                        1,"提示","暂存编辑？",
                        "否","是");
                noticeDialog.show();
                break;
            case R.id.getPwd:
                GetPwdOneActivity.start(this);
                break;
            case R.id.wxlogin:

//                    QQLogin();
                break;
            case R.id.top_mune2:


                saveData("0");

                break;
            case R.id.ll_zwr_db_addr:
                Intent intent = new Intent(this, CreateAddrActivity.class);
                intent.putExtra(CreateAddrActivity.KEY_FROM,CreateAddrActivity.FROM_FLOW);
                startActivityForResult(intent, REQUEST_CODE_ADDRESS);
                break;
            case R.id.top_mune1:

                noticeDialog = new PublicNoticeDialog(CreateProjectDetailThreeActivity.this, new View.OnClickListener() {
                    @Override
                    public void onClick( View view ) {
                        String  sendStatus = "";
                        if (view.getId() == R.id.btn_chancel_notice) {//暂存
                            sendStatus = "1";
                            noticeDialog.dismiss();
                        } else if (view.getId() == R.id.ll_close) {
                            if (noticeDialog != null && noticeDialog.isShowing())
                                noticeDialog.dismiss();
                            return;
                        } else {//生成
                            sendStatus = "2";
                            noticeDialog.dismiss();
                        }
                        saveData(sendStatus);
                        noticeDialog.dismiss();

                    }
                }, 1, "提示", "创建成功", "暂存退出", "项目创立");
                noticeDialog.show();
                break;
            case R.id.rl_addlogo:
//                showPopupWindow();
                addImageType = 1;
                pickAndUploadPic(urlList1, adapter);
//                ll_popup.startAnimation(AnimationUtils.loadAnimation(
//                        CreateProjectDetailThreeActivity.this, R.anim.activity_translate_in));
//                pop.showAtLocation(view, Gravity.BOTTOM, 0, 0);
                break;

            case R.id.rl_addlogo2:
//                showPopupWindow();
                addImageType = 2;
                pickAndUploadPic(urlList2, adapter2);
//                ll_popup.startAnimation(AnimationUtils.loadAnimation(
//                        CreateProjectDetailThreeActivity.this, R.anim.activity_translate_in));
//                pop.showAtLocation(view, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.rl_addlogo4:
//                showPopupWindow();
                addImageType = 4;
                pickAndUploadPic(urlList4, adapter4);
//                ll_popup.startAnimation(AnimationUtils.loadAnimation(
//                        CreateProjectDetailThreeActivity.this, R.anim.activity_translate_in));
//                pop.showAtLocation(view, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.rl_addlogo5:
//                showPopupWindow();
                addImageType = 5;
                pickAndUploadPic(urlList5, adapter5);
//                ll_popup.startAnimation(AnimationUtils.loadAnimation(
//                        CreateProjectDetailThreeActivity.this, R.anim.activity_translate_in));
//                pop.showAtLocation(view, Gravity.BOTTOM, 0, 0);
                break;
            default:
                break;
        }


    }

    public void saveData(final String status) {
        shapeLoadingDialog.setLoadingText("提交信息中...");
        shapeLoadingDialog.setCanceledOnTouchOutside(false);
        shapeLoadingDialog.show();
        if (BankruptcyApp.tokeninfo == null || BankruptcyApp.tokeninfo.getToken() == null) {
            getToken();
            return;
        }

        JSONArray jsonArray = new JSONArray();
        JSONArray jsonArray2 = new JSONArray();
        JSONArray jsonArray4 = new JSONArray();
        JSONArray jsonArray5 = new JSONArray();

        try {
            for (int i = 0; i < urlList1.size(); i++) {
                if (TextUtils.equals(urlList1.get(i).getFilename(), NAME_ADD)) {
                    continue;
                }
                JSONObject obj = new JSONObject();
                try {
                    if (FileUtil.isImage(urlList1.get(i).getFilename())) {
                        obj.putOpt("attachmentName", "相关附件" + (i + 1) + ".jpg");
                    } else {
                        obj.putOpt("attachmentName", urlList1.get(i).getFilename());
                    }
                    obj.putOpt("attachmentDownloadAddress", urlList1.get(i).getFilepath());
                    obj.putOpt("attachmentSize", urlList1.get(i).getFilesize());
                    obj.putOpt("loadUrl", urlList1.get(i).getLoadUrl());
                    jsonArray.put(obj);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            for (int i = 0; i < urlList2.size(); i++) {
                if (TextUtils.equals(urlList2.get(i).getFilename(), NAME_ADD)) {
                    continue;
                }
                JSONObject obj = new JSONObject();
                try {
                    if (FileUtil.isImage(urlList2.get(i).getFilename())) {
                        obj.putOpt("attachmentName", "相关附件" + (i + 1) + ".jpg");
                    } else {
                        obj.putOpt("attachmentName", urlList2.get(i).getFilename());
                    }
                    obj.putOpt("attachmentDownloadAddress", urlList2.get(i).getFilepath());
                    obj.putOpt("attachmentSize", urlList2.get(i).getFilesize());
                    obj.putOpt("loadUrl", urlList2.get(i).getLoadUrl());
                    jsonArray2.put(obj);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            for (int i = 0; i < urlList4.size(); i++) {
                if (TextUtils.equals(urlList4.get(i).getFilename(), NAME_ADD)) {
                    continue;
                }
                JSONObject obj = new JSONObject();
                try {
                    if (FileUtil.isImage(urlList4.get(i).getFilename())) {
                        obj.putOpt("attachmentName", "相关附件" + (i + 1) + ".jpg");
                    } else {
                        obj.putOpt("attachmentName", urlList4.get(i).getFilename());
                    }
                    obj.putOpt("attachmentDownloadAddress", urlList4.get(i).getFilepath());
                    obj.putOpt("attachmentSize", urlList4.get(i).getFilesize());
                    obj.putOpt("loadUrl", urlList4.get(i).getLoadUrl());
                    jsonArray4.put(obj);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }


            for (int i = 0; i < urlList5.size(); i++) {
                if (TextUtils.equals(urlList5.get(i).getFilename(), NAME_ADD)) {
                    continue;
                }
                JSONObject obj = new JSONObject();
                try {
                    if (FileUtil.isImage(urlList5.get(i).getFilename())) {
                        obj.putOpt("attachmentName", "相关附件" + (i + 1) + ".jpg");
                    } else {
                        obj.putOpt("attachmentName", urlList5.get(i).getFilename());
                    }
                    obj.putOpt("attachmentDownloadAddress", urlList5.get(i).getFilepath());
                    obj.putOpt("attachmentSize", urlList5.get(i).getFilesize());
                    obj.putOpt("loadUrl", urlList5.get(i).getLoadUrl());
                    jsonArray5.put(obj);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        String strTmp1 = jsonArray.toString();
        String strTmp2 = jsonArray2.toString();
        String strTmp4 = jsonArray4.toString();
        String strTmp5 = jsonArray5.toString();

        mInfo.setDebtorYyzzFileListStr(strTmp1);
        mInfo.setCourtSlwsFileListStr(strTmp2);
        mInfo.setCourtManagerFileListStr(strTmp4);
        mInfo.setOtherFileListStr(strTmp5);

        mInfo.setDebtorLegalRepresentative( item_zwr_db.getText().toString().trim());
        mInfo.setSysAddressId(addressId);

        OkHttpClient okHttpClient = new OkHttpClient();
        HashMap<String, String> params = new HashMap<String, String>();
        Random random = new Random();
        String rom = random.nextLong() + "";
        String timestamp = System.currentTimeMillis() + "";


        String sign = SignUtil.createSign(Consts.APPKEY, params);
        String userToken = SPUtils.getInstance(Consts.SPNAME).getString("userToken", "");

        RequestBody requestBody = new FormBody.Builder()
                .add("appid", "1000000001")
                .add("nonce", rom)
                .add("timestamp", timestamp)
                .add("token", BankruptcyApp.tokeninfo.getToken())
                .add("userToken", userToken)
//                .add("appVersion", "0.0.1")

                .add("addRole", mInfo.getAddRole())
                .add("id", mInfo.getId())
                .add("debtorName", mInfo.getDebtorName())
                .add("projectStatus", status)
                .add("projectType", mInfo.getProjectType())
                .add("projectName", mInfo.getProjectName())
                .add("projectLogo", mInfo.getProjectLogo())
                .add("projectNote", mInfo.getProjectNote())
                .add("courtName", mInfo.getCourtName())
                .add("caseNumber", mInfo.getCaseNumber())
                .add("debtorLegalRepresentative", mInfo.getDebtorLegalRepresentative())
                .add("sysAddressId", mInfo.getSysAddressId())
                .add("publicStatus", "1")
                .add("publicSection", mInfo.getPublicSection())
                .add("close", mInfo.getClose())
                .add("allowJoin", mInfo.getAllowJoin())
                .add("closeClaimFiling", mInfo.getCloseClaimFiling())
                .add("openDiscussion", mInfo.getOpenDiscussion())
                .add("allowCreditorsChangInfo", mInfo.getAllowCreditorsChangInfo())
                .add("displayedInstantly", mInfo.getDisplayedInstantly())
                .add("creditorsVisibleDetails", mInfo.getCreditorsVisibleDetails())
                .add("openAdministratorList", mInfo.getOpenAdministratorList())

                .add("openAdministratorInfo", mInfo.getOpenAdministratorInfo())
                .add("openCreditorsList", mInfo.getOpenCreditorsList())
                .add("openCreditorsInfo", mInfo.getOpenCreditorsInfo())

                .add("managerListStr", mInfo.getManagerListStr())
                .add("debtorYyzzFileListStr", mInfo.getDebtorYyzzFileListStr())
                .add("courtSlwsFileListStr", mInfo.getCourtSlwsFileListStr())
                .add("courtManagerFileListStr", mInfo.getCourtManagerFileListStr())
                .add("otherFileListStr", mInfo.getOtherFileListStr())
                .add("contacts", mInfo.getContacts())
                .add("contactPhone", mInfo.getContactPhone())
                .add("courtId",  mInfo.getCourtId())


                .build();

        String url = Consts.WEBURL_ADDPRO;
        if (!"".equalsIgnoreCase(mInfo.getId())) {
            url = Consts.WEBURL_UPDATEPRO;
        }
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
//                Log.d(TAG, "onFailure: " + e.getMessage());
                shapeLoadingDialog.dismiss();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                Log.d(TAG, response.protocol() + " " +response.code() + " " + response.message());
                Headers headers = response.headers();
                shapeLoadingDialog.dismiss();
                String str = response.body().string();
                try {
                    JSONObject object = new JSONObject(str);
                    if (object.optInt("code") == 0) {

                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                //更新UI
                                EventBus.getDefault().post(mInfo);
//                                Toast.makeText(CreateProjectDetailThreeActivity.this,
//                                        "提交成功", Toast.LENGTH_SHORT).show();
                                showToast("项目创建成功", ToastUtil.TOAST_TYPE.SUCCESS);
                                finish();

                            }

                        });
                    } else {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                //更新UI
                                Toast.makeText(CreateProjectDetailThreeActivity.this,
                                        "提交失败", Toast.LENGTH_SHORT).show();


                            }

                        });
                    }

                    str = "";
                } catch (Exception ex) {

                    ex.printStackTrace();
                }
            }
        });


    }


    private void initWidget() {
        FullyGridLayoutManager manager = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(manager);
        adapter = new GridImagesAdapter(CreateProjectDetailThreeActivity.this, TYPE_EDIT);
        adapter.setList(urlList1);
        adapter.setTag(1);
        adapter.setSelectMax(maxSelectNum);
        mRecyclerView.setAdapter(adapter);
    }

    private void initWidget2() {
        FullyGridLayoutManager manager = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        mRecyclerView2.setLayoutManager(manager);
        adapter2 = new GridImagesAdapter(CreateProjectDetailThreeActivity.this, TYPE_EDIT);
        adapter2.setList(urlList2);
        adapter2.setTag(2);
        adapter2.setSelectMax(maxSelectNum);
        mRecyclerView2.setAdapter(adapter2);
    }

    private void initWidget4() {
        FullyGridLayoutManager manager = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        mRecyclerView4.setLayoutManager(manager);
        adapter4 = new GridImagesAdapter(CreateProjectDetailThreeActivity.this, TYPE_EDIT);
        adapter4.setList(urlList4);
        adapter4.setTag(4);
        adapter4.setSelectMax(maxSelectNum);
        mRecyclerView4.setAdapter(adapter4);
    }

    private void initWidget5() {
        FullyGridLayoutManager manager = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        mRecyclerView5.setLayoutManager(manager);
        adapter5 = new GridImagesAdapter(CreateProjectDetailThreeActivity.this, TYPE_EDIT);
        adapter5.setList(urlList5);
        adapter5.setTag(5);
        adapter5.setSelectMax(maxSelectNum);
        mRecyclerView5.setAdapter(adapter5);
    }

    PopupWindow pop;
    LinearLayout ll_popup;

    public void showPopupWindow() {
        pop = new PopupWindow(CreateProjectDetailThreeActivity.this);
        View view = getLayoutInflater().inflate(R.layout.item_popupwindows,
                null);
        ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);
        pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
        pop.setContentView(view);
        RelativeLayout parent = (RelativeLayout) view.findViewById(R.id.parent);
        Button bt1 = (Button) view.findViewById(R.id.item_popupwindows_camera);
        Button bt2 = (Button) view.findViewById(R.id.item_popupwindows_Photo);
        Button bt3 = (Button) view.findViewById(R.id.item_popupwindows_cancel);
        parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
                ll_popup.clearAnimation();
            }
        });
        bt1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(camera, 1);
                    pop.dismiss();
                    ll_popup.clearAnimation();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent picture = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(picture, 2);
                pop.dismiss();
                ll_popup.clearAnimation();
            }
        });
        bt3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                pop.dismiss();
                ll_popup.clearAnimation();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_ADDRESS) {
                addressId = data.getStringExtra("id");
                item_zwr_db_addr.setText(data.getStringExtra("desc"));
            }
        }

        /**
         * 图片编辑
         */
        if (requestCode == ImageShowActivity.IMAGE_REQUEST && resultCode == Activity.RESULT_OK
                && null != data) {
            ArrayList urlList = data.getStringArrayListExtra("urlList");
            if (addImageType == 1) {
                urlList1.clear();
                urlList1.addAll(urlList);
                adapter.notifyDataSetChanged();
            } else if (addImageType == 2) {
                urlList2.clear();
                urlList2.addAll(urlList);
                adapter2.notifyDataSetChanged();
            } else if (addImageType == 4) {
                urlList4.clear();
                urlList4.addAll(urlList);
                adapter4.notifyDataSetChanged();
            } else if (addImageType == 5) {
                urlList5.clear();
                urlList5.addAll(urlList);
                adapter5.notifyDataSetChanged();
            }
        }
    }

    private  static int dialogType = 1;
    private PublicNoticeDialog noticeDialog;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {


        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            exit();
//            return false;
            dialogType =1;
            noticeDialog = new PublicNoticeDialog(CreateProjectDetailThreeActivity.this,onClickListener,
                    1,"提示","暂存编辑？",
                    "否","是");
            noticeDialog.show();
        }
        return super.onKeyDown(keyCode, event);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.ll_close:
                    noticeDialog.dismiss();
                    break;

                case R.id.btn_chancel_notice:
//                   Boolean flag =  noticelxDialog.cb_agree.isChecked();
                    if(dialogType ==1 ) {
                        finish();
                    }else{

                    }
                    noticeDialog.dismiss();

                    break;

                case R.id.btn_save_notice:
                    if(dialogType== 1) {
                        saveData("0");
                    }
                    noticeDialog.dismiss();
                    break;

            }
        }
    };
}

